import {getCouponList} from "@src/api/coupon";
import dayjs from "dayjs";

Component({
  data: {
    pageNum: 1,
    pageSize: 10,
    loading: false,
    listData: []
  },
  methods: {
    onLoad() {
      // todo 这里有坑, 过期和其他状态居然要分开拉, 还怎么分页?
      this.fetchData(2)
      this.fetchData(3)
    },
    onPullDownRefresh() {
      wx.stopPullDownRefresh();
      this.setData({pageNum: 1, listData: []})
      this.fetchData(2);
      this.fetchData(3);
    },
    onReachBottom() {
      this.setData({pageNum: this.data.pageNum + 1})
      this.fetchData(2);
      this.fetchData(3);
    },
    fetchData(status) {
      let data = {
        status: status,
        page: this.data.pageNum,
        pageSize: this.data.pageSize
      };
      this.setData({loading: true})
      getCouponList(data).then(res => {
        this.setData({loading: false})
        let list = res.data.list;
        list = list.map(value => {
          value.status = status;
          value.dillExpireTime = dayjs(value.customerCoupon.expireTime).subtract(1, "second").format("YYYY-MM-DD");
          return value;
        });
        this.setData({listData: this.data.listData.concat(list), hasNextPage: res.data.hasNextPage})
      }).catch(()=> {
        this.setData({loading: false})
      })
    }
  }
})
